package org.example;

/*
 * 词法解析器
 * */
public abstract class Lexer {
    public static final char EOF = (char) -1;
    public static final int EOF_TYPE = 1;

    protected String input;
    protected int p = 0; // 当前字符下标
    protected char c; // 当前的字符

    public Lexer(String input) {
        this.input = input;
        c = input.charAt(0);
    }

    // 消耗掉一个字符
    protected void consume() {
        p++;
        if (p >= input.length()) c = EOF;
        else c = input.charAt(p);
    }

    // 匹配字符，若成功则消耗一个字符，否则抛出异常
    protected void match(char x) {
        if (c == x) consume();
        else throw new Error("期望字符：" + x + ", 当前:" + c);
    }

    public abstract Token nextToken();

}
